﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
  
        /// <summary>
        /// A <see cref="IBusinessObjectRule"/> that validates that a relationship cannot be many to many 
        /// </summary>
        public class DMRelationshipOneSideMustBeSingle : IBusinessObjectRule
        {
            private DMRelationship _dmRelationship;

//            public DMRelationshipOneSideMustBeSingle(DMRelationship dmRelationship)
//            {
//                if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
//                _dmRelationship = dmRelationship;
//            }
            
            #region Implementation of IBusinessObjectRule

            /// <summary>
            /// Indicates whether the property value is valid against the rules
            /// </summary>
            /// <returns>
            /// Returns true if valid
            /// </returns>
            public bool IsValid()
            {
                throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
            }

            public bool IsValid(IBusinessObject bo)
            {
                _dmRelationship = (DMRelationship) bo;
                return IsOneSideSingle();
            }

            private bool IsOneSideSingle()
            {
                return _dmRelationship.IsSingle || (_dmRelationship.HasReverseRelationship 
                        && _dmRelationship.ReverseRelationship.IsSingle);
            }

            /// <summary>
            /// Returns the rule name
            /// </summary>
            public string Name
            {
                get { return "DMRelationshipOneSideMustBeSingle"; }
            }

            /// <summary>
            /// Returns the error message for if the rule fails.
            /// </summary>
            public string Message
            {
                get
                {
                    return string.Format("One side of the relationship '{0}-{1}' on Classes '{2}-{3}' must be Single", _dmRelationship, _dmRelationship.ReverseRelationship, _dmRelationship.ClassDM, _dmRelationship.RelatedClass);
                }
            }

            /// <summary>
            /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
            /// </summary>
            public ErrorLevel ErrorLevel
            {
                get { return Habanero.Base.ErrorLevel.Error; }
            }

            #endregion
        }
   
}
